package com.xq.jltx.fragment;

import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.xq.jltx.adapter.AddNoteAdapter;
import com.xq.jltx.db.NoteDbUtils;
import com.xq.jltx.db.bean.Note;
import com.xq.jltx.greendaopro.R;
import com.xq.jltx.utils.XqToast;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2016/07/15.
 */
public class AddFragment extends Fragment{

    private EditText noteEditText;
    private Button addBtn;
    private ListView noteListView;
    private AddNoteAdapter addNoteAdapter;

    public AddFragment() {
        super();
    }

    public static AddFragment newInstance(){
        AddFragment addFragment = new AddFragment();
        return addFragment;
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.add_fragment_layout,container,false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        assignView(view);
        addBtn.setOnClickListener(mOnClickListener);
        addNoteAdapter = new AddNoteAdapter(getContext(), NoteDbUtils.getAllNotesCursor());
        noteListView.setAdapter(addNoteAdapter);
    }

    private void assignView(View view){
        noteEditText = (EditText)view.findViewById(R.id.note_edittext);
        addBtn = (Button)view.findViewById(R.id.add_btn);
        noteListView = (ListView)view.findViewById(R.id.my_note_list);

    }

    private View.OnClickListener mOnClickListener = new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            if(view.getId() == R.id.add_btn){

                String text = noteEditText.getText().toString().trim();
                if(!TextUtils.isEmpty(text)){
                    SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date curDate=new Date();
                    String t1="创建于"+ format.format(curDate);
                    Note note = new Note(null,text,t1,curDate);
                    NoteDbUtils.addNote(note);
                    Cursor cursor = NoteDbUtils.getAllNotesCursor();
                    addNoteAdapter.swapCursor(cursor);
                }else {
                    XqToast.showToastShortMsg(getContext(),"输入的文字为空");
                }
            }
        }
    };
    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
    }
}
